package test.test.test;

/*
 * The changes in the options here are global
 */
import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.text.Text;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.font.FontFactory;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import test.test.R;

import android.graphics.Color;

public class Options extends BaseGameActivity {
	// ===========================================================
	// Constants
	// ===========================================================

	private static final int CAMERA_WIDTH = 720;
	private static final int CAMERA_HEIGHT = 480;
	// Creates Id's for the buttons
	protected static final int DIGTEXT = 0;
	protected static final int TOUCHTEXT = DIGTEXT + 1;
	private static final int FONT_SIZE = 52;
	// ===========================================================
	// Fields
	// ===========================================================
	//This is used in other classes
	public static boolean DigitalControlEnabled;
	public static boolean TouchEnabled=true;
	
	private Camera mCamera;
	private Scene scene;
	private Texture mFontTexture;
	private Font mFont;
	private MultiStateText DigText;
	private MultiStateText TouchText;
	private MultiStateText BackText;

	public void onLoadComplete() {

	}

	public Engine onLoadEngine() {
		// Initializes the Engine and sets the height and width
		this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		// Sets the orientation of the screen along with the resolution(Multiple
		// displays will see the same image)
		final EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE,
				new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera);
		// Sets the touch interactions to get processed during the update
		// thread??
		engineOptions.getTouchOptions().setRunOnUpdateThread(true);
		// Initializes the Engine
		Engine engine = new Engine(engineOptions);
		// Returns the Engine
		return engine;
	}

	public void onLoadResources() {
		/* Load Font/Textures. */
		this.mFontTexture = new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		FontFactory.setAssetBasePath("font/");
		this.mFont = FontFactory.createFromAsset(this.mFontTexture, this, "Zrnic.ttf", FONT_SIZE, true, Color.WHITE);
		this.mEngine.getTextureManager().loadTexture(this.mFontTexture);
		this.mEngine.getFontManager().loadFont(this.mFont);
	}

	public Scene onLoadScene() {
		this.mEngine.registerUpdateHandler(new FPSLogger());

		scene = new Scene();
		scene.setBackground(new ColorBackground(0.0f, 0.0f, 0.0f));

		//Calculate the coordinates for the text, so its in columns.		 
		final int InitialX = (CAMERA_WIDTH) / 16;
		final int InitialY = (CAMERA_HEIGHT) / 4;

		//Place Options onto Scene
		UIOptions(InitialX, InitialY, 0);

		//Adds the Back Button to the scene
		BackButton(InitialX);

		scene.setTouchAreaBindingEnabled(true);

		return scene;
	}

	// ===========================================================
	// Methods
	// ===========================================================

	/*
	 * This controls what happens when you touch the text. If you touch it the color will change, but
	 * will go back to normal if you move outside the text region. If you lift up in the region
	 * then the text will change and the effect will be applied
	 */
	private void UIOptions(int OptionX, int OptionY, int OptionNumber) {
		int OptionRow = OptionY + OptionNumber * FONT_SIZE;
		/* Create the face and add it to the scene. */
		final Text Control = new Text(OptionX, OptionRow, mFont, "Control Options:");
		//Text that controls the Digital options
		DigText = new MultiStateText(OptionX * 12, OptionRow, mFont, "Digital") {
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX,
					final float pTouchAreaLocalY) {

				//Does things depending on how you touch thext
				switch (pSceneTouchEvent.getAction()) {

				case TouchEvent.ACTION_DOWN:
					this.setState(ACTIVE);
					DigText.setColor(0.0f, 1.0f, 0.0f);
					break;

				case TouchEvent.ACTION_UP:
					this.setState(DEFAULT);
					DigText.setColor(1.0f, 1.0f, 1.0f);
					if (this.isActionOutside(pTouchAreaLocalX, pTouchAreaLocalY)) {
						// if outside do nothing
					} else {
						//Detaches the current text and puts the next one
						scene.detachChild(DigText);
						scene.attachChild(TouchText);
						DigitalControlEnabled = false;
						TouchEnabled = true;
						scene.unregisterTouchArea(this);
						scene.registerTouchArea(TouchText);
					}
					
					break;
				case TouchEvent.ACTION_MOVE:
					if (this.isActionOutside(pTouchAreaLocalX, pTouchAreaLocalY)) {
						DigText.setColor(1.0f, 1.0f, 1.0f);
					} else {
						DigText.setColor(0.0f, 1.0f, 0.0f);
					}
					break;
				}
				return true;
			}
		};
		//Text that controls the Touch Options
		TouchText = new MultiStateText(OptionX * 12, OptionRow, mFont, "Touch") {
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX,
					final float pTouchAreaLocalY) {

				switch (pSceneTouchEvent.getAction()) {

				case TouchEvent.ACTION_DOWN:
					this.setState(ACTIVE);
					TouchText.setColor(0.0f, 1.0f, 0.0f);
					break;
					
				case TouchEvent.ACTION_UP:
					this.setState(DEFAULT);
					TouchText.setColor(1.0f, 1.0f, 1.0f);
					if (this.isActionOutside(pTouchAreaLocalX, pTouchAreaLocalY)) {
					} else {
						//Detaches the current text and puts the next one
						scene.detachChild(TouchText);
						scene.attachChild(DigText);
						DigitalControlEnabled = true;
						TouchEnabled = false;
						scene.unregisterTouchArea(this);
						scene.registerTouchArea(DigText);
					}
					break;

				case TouchEvent.ACTION_MOVE:
					if (this.isActionOutside(pTouchAreaLocalX, pTouchAreaLocalY)) {
						TouchText.setColor(1.0f, 1.0f, 1.0f);
					} else {
						TouchText.setColor(0.0f, 1.0f, 0.0f);
					}
					break;

				}
				return true;
			}
		};

		scene.attachChild(Control);
		if (Options.TouchEnabled) {
			scene.attachChild(TouchText);
			scene.registerTouchArea(TouchText);
		}
		if (Options.DigitalControlEnabled) {
			scene.attachChild(DigText);
			scene.registerTouchArea(DigText);
		}
	}

	private void BackButton(int OptionX) {
		//Text that controls the Touch Options
		BackText = new MultiStateText(OptionX, 0.75f * (CAMERA_HEIGHT), mFont, "Back") {
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX,
					final float pTouchAreaLocalY) {

				//Deals with what happens if touched
				switch (pSceneTouchEvent.getAction()) {

				case TouchEvent.ACTION_DOWN:
					this.setState(ACTIVE);
					BackText.setColor(0.0f, 1.0f, 0.0f);
					break;
				case TouchEvent.ACTION_UP:
					this.setState(DEFAULT);
					BackText.setColor(1.0f, 1.0f, 1.0f);
					if (this.isActionOutside(pTouchAreaLocalX, pTouchAreaLocalY)) {
						//Do nothing if touched outside
					} else {
						finish();
					}
					break;

				case TouchEvent.ACTION_MOVE:
					if (this.isActionOutside(pTouchAreaLocalX, pTouchAreaLocalY)) {
						BackText.setColor(1.0f, 1.0f, 1.0f);
					} else {
						BackText.setColor(0.0f, 1.0f, 0.0f);
					}
					break;

				}
				return true;
			}
		};
		scene.attachChild(BackText);
		scene.registerTouchArea(BackText);
	}

	// ==========================================================
	// Inner and Anonymous Classes
	// ===========================================================

	// Transitions done when an activity is pushed to the background
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Options.this.overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
	}

}
